/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on 2011-03-22, 16:58:19
 */

package trumpet.gui;

import java.awt.event.MouseEvent;
import trumpet.connection.Connection;
import trumpet.connection.Server;
import trumpet.core.*;
import trumpet.gui.buddylist.BuddyList;

/**
 *
 * @author michal
 */
public class Window extends javax.swing.JFrame {

    /** Creates new form Window */
    public Window() {
        initComponents();
        initBuddyList();
    }

    public BuddyList getBuddyList()
    {
        return FriendsList;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FriendsScrollPanel = new javax.swing.JScrollPane();
        MainMenu = new javax.swing.JMenuBar();
        FriendsMenu = new javax.swing.JMenu();
        AddFriendItem = new javax.swing.JMenuItem();
        EditFriendItem = new javax.swing.JMenuItem();
        RemoveFriendItem = new javax.swing.JMenuItem();
        OptionsMenu = new javax.swing.JMenu();
        HelpListItem = new javax.swing.JMenuItem();
        AboutItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Trumpet");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        FriendsMenu.setText("Znajomi");

        AddFriendItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        AddFriendItem.setText("Dodaj");
        AddFriendItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddFriendItemActionPerformed(evt);
            }
        });
        FriendsMenu.add(AddFriendItem);

        EditFriendItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        EditFriendItem.setText("Edytuj");
        EditFriendItem.setEnabled(false);
        EditFriendItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditFriendItemActionPerformed(evt);
            }
        });
        FriendsMenu.add(EditFriendItem);

        RemoveFriendItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        RemoveFriendItem.setText("Usuń");
        RemoveFriendItem.setEnabled(false);
        RemoveFriendItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveFriendItemActionPerformed(evt);
            }
        });
        FriendsMenu.add(RemoveFriendItem);

        MainMenu.add(FriendsMenu);

        OptionsMenu.setText("Opcje");
        OptionsMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OptionsMenuActionPerformed(evt);
            }
        });

        HelpListItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        HelpListItem.setText("Pomoc");
        HelpListItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HelpListItemActionPerformed(evt);
            }
        });
        OptionsMenu.add(HelpListItem);

        AboutItem.setText("O programie");
        AboutItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AboutItemActionPerformed(evt);
            }
        });
        OptionsMenu.add(AboutItem);

        MainMenu.add(OptionsMenu);

        setJMenuBar(MainMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FriendsScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FriendsScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddFriendItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddFriendItemActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                final FriendEdit addFriendFrame = new FriendEdit();
                addFriendFrame.setVisible(true);
                addFriendFrame.getSaveButton().addMouseListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        FriendsList.save(addFriendFrame.getUser());
                        addFriendFrame.setVisible(false);

                    }
                });
                
                getContentPane().invalidate();
                getContentPane().validate();
//                    new
                    //new FriendEdit().setVisible(true);
            }
        });
    }//GEN-LAST:event_AddFriendItemActionPerformed

    private void EditFriendItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditFriendItemActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                final FriendEdit friend = new FriendEdit(FriendsList.getSelectedIndex());
                friend.setUser(FriendsList.getSelectedUser()).populate();
                friend.setVisible(true);
                friend.getSaveButton().addMouseListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        FriendsList.save(friend.getUser());
                        friend.setVisible(false);
                    }
                });

                getContentPane().invalidate();
                getContentPane().validate();
            }
        });
    }//GEN-LAST:event_EditFriendItemActionPerformed

    private void RemoveFriendItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveFriendItemActionPerformed
       java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                    FriendsList.delete();
                    getContentPane().invalidate();
                    getContentPane().validate();
            }
        });
    }//GEN-LAST:event_RemoveFriendItemActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
       UserCollection collection = (UserCollection) FriendsList.getModel();
       collection.getAdapter().save();
    }//GEN-LAST:event_formWindowClosing

    private void AboutItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutItemActionPerformed
        new About().setVisible(true);
    }//GEN-LAST:event_AboutItemActionPerformed

    private void OptionsMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OptionsMenuActionPerformed
        
    }//GEN-LAST:event_OptionsMenuActionPerformed

    private void HelpListItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HelpListItemActionPerformed
        new Help().setVisible(true);
    }//GEN-LAST:event_HelpListItemActionPerformed


    private void initBuddyList() {
        FriendsList = new BuddyList();
        FriendsList.setModel(new UserCollection());
        FriendsList.setVisibleRowCount(10);

        FriendsScrollPanel.setViewportView(FriendsList);


//        FriendsList.setSelectedIndex(index);
//        FriendsList.ensureIndexIsVisible(index);

        // dodanie akcji na dwuklik myszy
        FriendsList.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BListMouseClicked(evt);
            }
        });

        pack();
    }
    private void BListMouseClicked(java.awt.event.MouseEvent evt) {
        activateContextMenu(true);

        if (evt.getClickCount() > 1) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                   User u = FriendsList.getSelectedUser();
                   Connection c = Server.getInstance().createClientConnection(u);
                   c.handle();
                    //new Message().setVisible(true);
                }
            });
        }
    }

    private void activateContextMenu(boolean enable) {
        EditFriendItem.setEnabled(enable);
        RemoveFriendItem.setEnabled(enable);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AboutItem;
    private javax.swing.JMenuItem AddFriendItem;
    private javax.swing.JMenuItem EditFriendItem;
    private javax.swing.JMenu FriendsMenu;
    private javax.swing.JScrollPane FriendsScrollPanel;
    private javax.swing.JMenuItem HelpListItem;
    private javax.swing.JMenuBar MainMenu;
    private javax.swing.JMenu OptionsMenu;
    private javax.swing.JMenuItem RemoveFriendItem;
    // End of variables declaration//GEN-END:variables

    private trumpet.gui.buddylist.BuddyList FriendsList;
}
